package com.uaroadshow.finale.web.controller.api;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;
import com.uaroadshow.entity.trade.Product;
import com.uaroadshow.entity.trade.ProductStat;
import com.uaroadshow.finale.service.ProductService;

/**
 * 产品
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping("/api/products")
public class ProductApiController {
    
    private static final Logger logger = LoggerFactory.getLogger(ProductApiController.class);
    
    @Autowired
    private ProductService productService;
    
    /**
     * 产品列表
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult list() {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            List<Product> products = productService.list();
            
            httpResult.success().data("products", products)
                                .message("查询成功");
            
        } catch (Exception e) {
            logger.error("#list() - list product fail，exception=[{}]", e);
            httpResult.fail("500").message("查询失败");
        }
        
        return httpResult;
        
    }

    /**
     * 产品详情
     * 
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult view(@PathVariable("id") Long id) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            Product product = productService.load(Product.class, id);
            
            httpResult.success().data("product", product)
                                .message("查询成功");
            
        } catch (Exception e) {
            logger.error("#view() - load product fail，exception=[{}]", e);
            httpResult.fail("500").message("查询失败");
        }
        
        return httpResult;
        
    }
    
    /**
     * 产品库存余额
     * 
     * @return
     */
    @RequestMapping(value = "/remaining", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult remaining() {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            Map<String, ProductStat> remaining = productService.getRemaining();
            
            httpResult.success().data("remaining", remaining);
            
        } catch (Exception e) {
            logger.error("#remaining() - load product remaining fail，exception=[{}]", e);
            httpResult.fail("500").message("查询失败");
        }
        
        return httpResult;
        
    }
    
}